package com.jari.lg.service.analysis.guide;

import com.jari.lg.domain.DevBase;
import com.jari.lg.domain.GuideHis;
import com.jari.lg.domain.SysDictData;
import com.jari.lg.domain.SysDictDataExample;
import com.jari.lg.mapper.GuideHisMapper;
import com.jari.lg.mapper.SysDictDataMapper;
import com.jari.lg.utils.BaseData;
import com.jari.lg.utils.Utils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
* 版 权 : jariec.com
* 包 名 : com.jari.lg.service.analysis.guide
* 描 述 : 生成诱导原始数据服务
* 创建 时 间: 2022/3/25 15:41
* @author : lpf
*/
@Service
public class GuideService {

    @Resource
    private GuideHisMapper guideHisMapper;

    @Resource
    private SysDictDataMapper sysDictDataMapper;


    /**
    * @描述 生成诱导原始数据
    * @时间 2022/3/25 15:44
    * @参数
    * @返回值
    * @Author lpf
    */
    @Scheduled(cron = "30 * * * * ? ")
    public void createGuideHisData() throws ParseException {

        List<GuideHis>  result = new ArrayList<>();
        List<SysDictData> sysDictDataList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            GuideHis guideHis = new GuideHis();
            Date date = randomDate("2021-01-01","2021-12-31");
            String datetime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date);
            String date1 = new SimpleDateFormat("yyyy.MM.dd").format(date);
            Date datetime1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(datetime);
            DevBase guidedev = BaseData.guideList.get(new Random().nextInt(BaseData.guideList.size()));
            SysDictDataExample example = new SysDictDataExample();
            SysDictDataExample.Criteria criteria = example.createCriteria();
            criteria.andDicttypeEqualTo("GUIDE_PROGRAM_TYPE");
            sysDictDataList = sysDictDataMapper.selectByExample(example);
            String id = Utils.getUUID();
            String guideId = null;
            String guidename = null;
            String guidedisplaytype = null;
            String guideprogram = null;
            int guideprogramtime = 0;
            String guideprogramtype = sysDictDataList.get(new Random().nextInt(sysDictDataList.size())).getDictdata();
            if (guidedev.getDevicename().contains("停车场")) {
                guideId = guidedev.getDevicecode();
                guidename = guidedev.getDevicename();
                guidedisplaytype = "1";
                guideprogramtype = "3";
                guideprogram = date1 + guidename + "停车诱导节目";
                guideprogramtime = (int) (Math.random()*(1000 - 500 + 1));
            } else {
                switch (guideprogramtype) {
                    case "1":
                        guideId = guidedev.getDevicecode();
                        guidename = guidedev.getDevicename();
                        guidedisplaytype = "2";
                        guideprogram = date1 + guidename + "路况诱导节目";
                        guideprogramtime = (int) (Math.random()*(1000 - 500 + 1));
                        break;
                    case "2":
                        guideId = guidedev.getDevicecode();
                        guidename = guidedev.getDevicename();
                        guidedisplaytype = "2";
                        guideprogram = date1 + guidename + "路况诱导节目";
                        guideprogramtime = (int) (Math.random()*(1000 - 500 + 1));
                        break;
                    default:
                        break;
                }
            }
            guideHis.setId(id);
            guideHis.setGuideid(guideId);
            guideHis.setGuidename(guidename);
            guideHis.setGuidetype(guidedisplaytype);
            guideHis.setGuideprogram(guideprogram);
            guideHis.setGuideprogramtime(guideprogramtime);
            guideHis.setGuideprogramtype(guideprogramtype);
            guideHis.setDatetime(datetime1);
            if (guidename != null) {
                result.add(guideHis);
            }
        }
        guideHisMapper.insertBatch(result);
    }

    /**
     * @描述 生成指定时间内的随机时间
     * @时间 2022/3/22 18:20
     * @参数
     * @返回值
     * @Author lpf
     */
    private static Date randomDate(String beginDate, String endDate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);

            if(start.getTime() >= end.getTime()){
                return null;
            }
            long date = random(start.getTime(),end.getTime());
            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end) {
            return random(begin,end);
        }
        return rtn;
    }
}
